package com.cxh.dataservice.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.cxh.dataservice.config.DataSourceStrategyFactory;
import com.cxh.dataservice.entity.SysUserTest;
import com.cxh.dataservice.mapper.SysUserTestMapper;
import com.cxh.dataservice.service.DataSourceService;
import com.cxh.dataservice.service.TestService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * <p>
 * 用户信息表 服务实现类
 * </p>
 *
 * @author ChenXH
 * @since 2020-07-03
 */
@Service("sysUserServiceTest")
@DS("slave_1")
public class TestServiceImpl extends ServiceImpl<SysUserTestMapper, SysUserTest> implements TestService, InitializingBean{

    @Autowired
    private SysUserTestMapper sysUserTestMapper;

    @Override
    public List<Map<String, Object>> getData(String sql, HashMap<String, String> map) {
        if (!map.isEmpty()) {
            Set<String> strings = map.keySet();

            for (String s : strings) {
                sql = sql.replaceAll(":" + s, map.get(s));
            }
        }

        return sysUserTestMapper.query(sql);
    }

    @Override
    public List<Map<String, Object>> getTestData(String sql) {
        return sysUserTestMapper.getTestData(sql);
    }

    @Override
    public List<Map<String, Object>> getData(String sql) {
        return sysUserTestMapper.query(sql);
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        DataSourceStrategyFactory.setData("test", this);
    }
}
